<template>
  <div class="studentList">
    <!-- 顶部新增 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="small"
    >
      <el-form-item>
        <el-button type="primary" @click="addstudents">新增</el-button>
      </el-form-item>

      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="学号">
        <el-input v-model="formInline.number" placeholder="学号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="find">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="sex" label="性别" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column prop="number" label="学号" align="center">
      </el-table-column>
      <el-table-column prop="class" label="班级" align="center">
      </el-table-column>
      <el-table-column prop="state" label="状态" align="center">
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="130px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="updataInfo(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="delData(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增信息弹窗 -->
    <el-dialog
      :title="state?'添加学生信息':'修改学生信息'"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
          <el-input v-model.number="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学号" :label-width="formLabelWidth" prop="number">
          <el-input v-model.number="form.number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="班级" :label-width="formLabelWidth" prop="class">
          <el-select v-model="form.class" placeholder="请选择班级">
            <el-option label="H5-2201" value="H5-2201"></el-option>
            <el-option label="H5-2202" value="H5-2202"></el-option>
            <el-option label="H5-2203" value="H5-2203"></el-option>
            <el-option label="H5-2204" value="H5-2204"></el-option>
            <el-option label="H5-2205" value="H5-2205"></el-option>
            <el-option label="H5-2206" value="H5-2206"></el-option>
            <el-option label="H5-2207" value="H5-2207"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="state">
          <el-select v-model="form.state" placeholder="请选择班级">
            <el-option label="已入学" value="已入学"></el-option>
            <el-option label="休学中" value="休学中"></el-option>
            <el-option label="未入学" value="未入学"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth" prop="address">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="联系方式"
          :label-width="formLabelWidth"
          prop="phone"
        >
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "StudentList",
  props: [""],
  data() {
    return {
      //表格信息
      tableData: [],
     
      formInline: {
        user: "",
        number: "",
      },
      dialogFormVisible: false,
       state:true,
      //表单信息
      form: {
        name: "",
        sex: "男",
        class: "H5-2204",
        state: "已入学",
        address: "",
        phone: "",
        age: "",
        number: "",
      },
      formLabelWidth: "80px",
      //表单验证
      rules: {
        name: [{ required: true, message: "请输入姓名" }],
        sex: [{ required: true}],
        age: [{ required: true, message: "请输入年龄" },{type:'number',message:'年龄必须是数字'}],
        number: [{ required: true, message: "请输入学号" },{type:'number',message:'学号必须是数字'}],
        class: [{ required: true}],
        state: [{ required: true}],
        address: [{ required: true, message: "请输入您的住址" }],
        phone: [{ required: true, message: "请输入联系方式" }],
      },
    };
  },

  components: {},

  computed: {},

  created() {
     this.getData()
  },

  mounted() {},

  methods: {
    //获取表格信息
    getData(){
       this.servers.get('/msg',this.form).then((res) => {
              // console.log(res);
              if(res.status==200){
        
                this.tableData=[res.data.data]
              }
            }).catch((err) => {
              console.log(res);
            })
    },
    //修改数据的方法
    updataInfo(row) {
      console.log(row),
      //显示弹窗状态
      this.state=false
      this.dialogFormVisible = true
      this.form=row

    },
    //删除数据的方法
    delData(row) {
      // console.log(row);
              this.$alert('你确定删除么', '删除提示', {
          confirmButtonText: '确定',
          callback: () => {
            //请求删除
            this.$message({
                  message: "恭喜你删除成功",
                  type: "success",
                });
            this.servers.delete('/delete'+row).then((res) => {
              // console.log(res);
              if(res.status==200){
        
                this.tableData=[res.data.data]
                getData()
              }
            }).catch((err) => {
              console.log(res);
            })
          }
        });
      
    },
    //新增的方法this.state=false,
    addstudents() {
      //显示弹窗
      this.state=true;
      this.dialogFormVisible = true;
      
    },
    //查询的方法
    find() {
      // console.log(this.formInline);
    },
    sure(form) {
      // console.log(this.form);
      this.$refs[form].validate((result) => {
        if (result) {
          //通过调用新增信息接口
          this.tableData.push(this.form)
          this.dialogFormVisible = false
          this.form={}
          if(this.state){
            //如果是ture调用添加接口
           
          }else{
            //调用修改的接口
          }
        }else{
          console.error(this.form);
        }
      })
    },
  },
};
</script>
<style lang='less' scoped>
.studentList {
  .el-select {
    width: 100%;
  }
  .dialog-footer {
    text-align: center;
  }
}
</style>